var materialDetail;

init();

function init() {
    //1.查询进度列表
    queryMaterialList();
}


/**
 * 列表
 */
function queryMaterialList(pageIndex) {
    var name = "";
    name = $("#name").val();
    if(pageIndex == null){
        pageIndex = 0;
    }
    $.ajax({
        url :   prefixUrl + "material/queryList",
        type:   "post",
        data:JSON.stringify({
            materialName : name,
            pageIndex : pageIndex
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            var list = data.data.rows;
            var materialHtml  = '<thead>'
                +'<tr>'
                +'<th>序号</th>'
                +'<th>材料名称</th>'
                +'<th>材料类型名称</th>'
                +'<th>材料备注</th>'
                +'<th>操作</th>'
                +'</tr>'
                +'</thead>'
                +'<tbody>';
            for(var i=0; i<list.length; i++){
                var desc = list[i].materialDesc;if(desc.length > 30){ desc =  desc.substr(0,29) + "..."}
                materialHtml = materialHtml
                    +'<tr>'
                    +'<td>' + (parseInt(i) + 1) + '</td>'
                    +'<td>'
                    +list[i].materialName
                    +'</td>'
                    +'<td>'
                    +list[i].materialTypeName
                    +'</td>'
                    +'<td>'
                    +desc
                    +'</td>'
                    +'<td>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#updateMaterial" data-whatever="@mdo" onclick=queryMaterialDetail("'
                    + list[i].materialId
                    +'")><i class="fa fa-pencil"></i></button>'
                    +'<button type="button" class="btn btn-info" onclick=deleteMaterial("'
                    +list[i].materialId
                    +'")><i class="fa fa-bitbucket-square"></i></button>'
                    +'</td>'
                    +'</tr>';
            }
            materialHtml = materialHtml + +'</tbody>';
            $("#materialList").html(materialHtml);


            var total = data.data.total;
            var remainder = total%20;
            var allNumber = 0;
            if(remainder > 0){
                allNumber = parseInt(total/20) + 1;
            }else{
                allNumber = parseInt(total/20);
            }
            var number = 1;
            var numberHtml = '<li><a onclick=queryMaterialList('+ "0" +')>«</a></li>';
            for(var i=0; i<allNumber; i++){
                var id = "query-" + i;
                if(i == 0){
                    numberHtml = numberHtml
                        +'<li id="'+ id + '" name="queryList" class="active"><a onclick=queryMaterialList('+ i +')>' + number + '</a></li>';
                }else{
                    numberHtml = numberHtml
                        +'<li id="'+ id + '" name="queryList"><a onclick=queryMaterialList('+ i +')>' + number + '</a></li>';
                }
                number++;
            }
            numberHtml = numberHtml + '<li><a onclick=queryMaterialList('+ (allNumber-1) +')>»</a></li>';
            $("#queryList").html(numberHtml);
        }
    });
}

function queryMaterialDetail(id){
    $.ajax({
        url :   prefixUrl + "material/queryDetail",
        type:   "post",
        data:JSON.stringify({
            materialId : id
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            var detail = data.data;
            materialDetail = detail;
            console.log(data.data);
            $("#materialName").val(detail.materialName);
            $("#materialDesc").val(detail.materialDesc);
            var typeParentCode = detail.typeParentCode;
            var typeCode = typeParentCode.substring(0,typeParentCode.length -2);
            $("#firstMaterial").val(typeCode);
            initMaterialTwoType(typeCode);
            $("#twoMaterial").val(detail.materialTypeId);
        }
    });
}

function updateMaterial(){
    var updateData = {
        materialId:  materialDetail.materialId,
        materialName:$("#materialName").val(),
        materialTypeId:$("#twoMaterial").val(),
        materialTypeName:$("#twoMaterial").find("option:selected").text(),
        materialDesc:$("#materialDesc").val()
    };
    $.ajax({
        url :   prefixUrl + "material/update",
        type:   "post",
        data:JSON.stringify(updateData),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            alert(data.msg);
            modalDisplay();
            queryMaterialList(0);
        }
    });
}

function deleteMaterial(id) {
    if(confirm("确定要进行删除嘛?")){
        $.ajax({
            url :   prefixUrl + "material/delete",
            type:   "post",
            data:JSON.stringify({materialId:id}),
            contentType:"application/json;",
            success:function(data){
                goToLogin(data);
                alert(data.msg);
                queryMaterialList(0);
            }
        });
    }
}
